package com.zzh.partnersys.im.handler;

import cn.hutool.extra.spring.SpringUtil;
import com.google.gson.Gson;
import com.zzh.partnersys.im.message.response.ApplyAddFriendResponseMessage;
import com.zzh.partnersys.im.session.SessionFactory;
import com.zzh.partnersys.im.rocketmq.contant.MQConstant;
import com.zzh.partnersys.im.rocketmq.producer.MessageSender;
import com.zzh.partnersys.im.sensitiveword.algorithm.SensitiveWordBs;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ??????
 * @version 1.0
 */
//专门只处理ApplyAddFriendResponseMessage类型的消息的Handler,处理好友申请列表（同意还是拒绝）
@Sharable
public class ApplyAddFriendResponseMessageHandler extends SimpleChannelInboundHandler<ApplyAddFriendResponseMessage> {
    Gson gson = new Gson();
    private MessageSender messageSender;
    private SensitiveWordBs sensitiveWordBs;

    @Override
    @Transactional
    public void channelRead0(ChannelHandlerContext channelHandlerContext, ApplyAddFriendResponseMessage applyAddFriendResponseMessage) throws Exception {
        System.out.println("applyAddFriendResponseMessage"+ applyAddFriendResponseMessage);
        //读取数据，发送消息内??
        if (null == applyAddFriendResponseMessage){
            return;
        }
        // 1->3 发送申??=>  3->1 回复申请
        Long toUid = applyAddFriendResponseMessage.getToUid();//1
        String content = applyAddFriendResponseMessage.getContent();

        //过滤敏词汇检??
        if (sensitiveWordBs == null){
            sensitiveWordBs = getSensitiveWordBs();
        }
        //过滤敏词??
        applyAddFriendResponseMessage.setContent(sensitiveWordBs.filter(content));


        //判断是否在线
        Channel channel = SessionFactory.getSession().getChannel(toUid);
        if (channel != null){
            //在线直接发送消??
            System.out.println("在线");
            //要以TextWebSocketFrame帧的形式写才??WebSocket)
            String json = gson.toJson(applyAddFriendResponseMessage);
            channel.writeAndFlush(new TextWebSocketFrame(json));
        }

        //rocketMQ发送消??
        if (messageSender == null){
            messageSender = getMessageSender();
        }
        messageSender.sendMsg(MQConstant.ADD_FRIEND_RES_TOPIC,applyAddFriendResponseMessage);




//        if (userApplyService == null){
//            userApplyService = getUserApplyService();
//        }
//        if (userFriendService == null){
//            userFriendService = getUserFriendService();
//        }
//
//        //user_apply??修改表数??
//        boolean isUpdate = userApplyService.updateApplyData(toUid,fromUid,content);
//        //好友列表添加
//        if ("同意".equals(content)){
//            boolean isInsert = userFriendService.insertUserFriend(fromUid,toUid,fromName, toName);
//        }
//
//
//        if (!isUpdate){
//            System.out.println("失败");
//            //插入失败(申请加入好友失败)
//            //todo {type:4,uid:fromUid,content:失败消息}=>转成json
////            channelHandlerContext.writeAndFlush(new TextWebSocketFrame(content));
//        }
    }

    protected MessageSender getMessageSender(){
        return SpringUtil.getBean(MessageSender.class);
    }


    //获取Bean对象
    protected SensitiveWordBs getSensitiveWordBs(){
        return SpringUtil.getBean(SensitiveWordBs.class);
    }


}
